<?php

namespace Model;

class channels extends \Zend_Db_Table_Abstract
{
    /**
     * @var string
     */
    protected $_name = 'channels';
    
    /**
     * Initialize the channels for a user. It should
     * only use for immediately after the registration!
     * 
     * @param type $userId
     * @return integer
     */
    public function insertChannelDefault($userId)
    {           
        $stmt = $this->getAdapter()->query(
        "INSERT INTO {$this->_name} SET
                userId = ?;",
        array($userId));
            
        return $stmt->getDriverStatement()->insert_id;
    }
    
    /**
     * @param int $userId
     * @param string $accessToken
     * @return int
     */
    public function addChannelFacebook($userId, $accessToken)
    {
        $stmt = $this->getAdapter()->query(
        "UPDATE {$this->_name} SET
                facebook = ?
                WHERE userId = ?;",
        array($accessToken, $userId));
            
        return $stmt->getDriverStatement()->affected_rows;
    }
    
    /**
     * @param int $userId
     * @return int
     */
    public function removeChannelFacebook($userId)
    {
        $stmt = $this->getAdapter()->query(
        "UPDATE {$this->_name} SET
                facebook = NULL
                WHERE userId = ?;",
        array($userId));
            
        return $stmt->getDriverStatement()->affected_rows;
    }
    
    /**
     * @param string $userId
     * @param array $channels
     * @return array
     */
    public function getChannels($userId, array $channels = array())
    {
        if (empty($channels))
        {
            $stmt = $this->getAdapter()->query(
            "SELECT facebook, foursquare
                    FROM {$this->_name}
                    WHERE `userId` = ?
                    LIMIT 1;",
            array($userId));
        
            return $stmt->fetch();
        }
        else
        {
            $selectQuery = implode(',', $channels);
            
            $stmt = $this->getAdapter()->query(
            "SELECT {$selectQuery}
                    FROM {$this->_name}
                    WHERE `userId` = ?
                    LIMIT 1;",
            array($userId));
        
            return $stmt->fetch();
        }
    }
    
    /**
     * @param int $userId
     * @return array
     */
    public function ctrlGetChannelsInfo($userId)
    {
        $result = array('facebook'      => NULL,
                        'foursquare'    => NULL);
        
        $channelsToken =
        $this->getChannels($userId);
        
        if ($channelsToken['facebook'])
        {
            $oFacebook  = new \cAppFacebook(\dtoFacebookAuth::factory());
            $fbCfg      = $oFacebook->getCfg();
            $oFacebookFetch = new \OAuth(   $fbCfg['key']['consumer_key'],
                                            $fbCfg['key']['consumer_secret'],
                                            \OAUTH_SIG_METHOD_HMACSHA1,
                                            \OAUTH_AUTH_TYPE_URI);
            $oFacebookFetch->setToken(  $channelsToken['facebook'],
                                        $fbCfg['key']['consumer_secret']);
            try
            {
                $query = $fbCfg['fetch']['me'] . '?' .
                        \http_build_query(
                            array('access_token' => $channelsToken['facebook']));
                $oFacebookFetch->fetch($query);
                $extras = \json_decode($oFacebookFetch->getLastResponse());
                
                $result['facebook'] = array('id'        => $extras->id,
                                            'name'      => $extras->name,
                                            'username'  => $extras->username);
            }
            catch (\Exception $e)
            {
                $this->removeChannelFacebook($userId);
            }
        }
        if ($channelsToken['foursquare'])
        {
            
        }
        
        return $result;
    }
}

# eof